import { Component, Input, OnInit, Output, EventEmitter } from '@angular/core';
import { NzMessageService, NzUploadFile } from 'ng-zorro-antd';
import { Observable, Observer } from 'rxjs';
import { UploadService } from 'src/app/services/upload.service';
import { AuditService } from '../audit.service';
import { AuditItem } from '../model/auditItem';

@Component({
  selector: 'zepride-audit-result',
  templateUrl: './audit-result.component.html',
  styleUrls: ['./audit-result.component.less']
})
export class AuditResultComponent implements OnInit {

  @Input() auditResult: AuditItem;
  @Output() auditResultChange = new EventEmitter<AuditItem>();
  @Input() readonly = false;
  auditItems: AuditItem[] = [];
  selectedAuditItem: AuditItem;

  private _allAuditItems: AuditItem[] = [];
  @Input() set allAuditItems(value: AuditItem[]) {
    this.auditItems = value.filter(x => x.nodeType !== 'submit');
    this._allAuditItems = value;
  }

  get allAuditItems() {
    return this._allAuditItems;
  }

  get canBackAuditItems() {
    return this._allAuditItems.filter(x => x.conclusion === 'pass');
  }

  @Input() backAuditItemIndex = 0;
  @Output() backAuditItemIndexChange = new EventEmitter<number>();
  private loading = false;
  uploadFileName = '';
  @Input() isAuthorized = true;

  constructor(
    public uploadService: UploadService,
    protected msg: NzMessageService,
    public auditSvr: AuditService
  ) { }

  ngOnInit(): void {
  }

  backAuditItemChange(value) {
    this.backAuditItemIndex = value;
    this.backAuditItemIndexChange.emit(this.backAuditItemIndex);
  }

  beforeUpload = (file: NzUploadFile, _fileList: NzUploadFile[]) => {
    return new Observable((observer: Observer<boolean>) => {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.msg.error('文件大小不能超过 2MB!');
        observer.complete();
        return;
      }
      observer.next(isLt2M);
      observer.complete();
    });
  }

  handleChange(info: { file: NzUploadFile }): void {
    switch (info.file.status) {
      case 'uploading':
        this.loading = true;
        break;
      case 'done':
        this.getBase64(info.file.originFileObj, (img: string) => {
          this.loading = false;
          this.auditResult.opinionValue = `${this.uploadService.uploadUrl}${info.file.response.fileUrl}`;
          this.uploadFileName = info.file.name;
          this.msg.success(`${info.file.name} 上传成功！`);
        });
        break;
      case 'error':
        this.msg.error('Network error');
        this.loading = false;
        break;
    }
  }

  private getBase64(img: File, callback: (img: string) => void): void {
    const reader = new FileReader();
    reader.addEventListener('load', () => callback(reader.result.toString()));
    reader.readAsDataURL(img);
  }

  cleanUploadFile() {
    this.uploadFileName = '';
    this.auditResult.opinionValue = '';
  }

  auditItemChange(value) {
    this.auditResult = value;
    this.auditResultChange.emit(this.auditResult);
  }

  auditResultChanged() {
    this.auditSvr.auditResultChanged.next(this.auditResult);
  }
}
